package com.nuaa.shopping.view;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;

import com.nuaa.shopping.entity.Item;
import com.nuaa.shopping.entity.Order;
import com.nuaa.shopping.entity.Product;
import com.nuaa.shopping.entity.User;
import com.nuaa.shopping.exception.ProductNotExistsException;
import com.nuaa.shopping.service.OrderService;
import com.nuaa.shopping.service.ProductService;
import com.nuaa.shopping.service.impl.OrderServiceImpl;
import com.nuaa.shopping.service.impl.ProductServiceImpl;
import com.nuaa.shopping.util.StringUtil;
import com.nuaa.shopping.vo.Cart;

public class UserView {
	private Scanner input = new Scanner(System.in);
	private ProductService productService = new ProductServiceImpl();
	private OrderService orderService = new OrderServiceImpl();
	private Cart cart = new Cart();
	private User user; // 当前登陆的用户
	
	public UserView(User u) {
		this.user = u;
	}

	/*
	 * 用户主菜单
	 */
	public void showMainMenu(){
		System.out.println("------------------------------------------");
		System.out.println("1.购买商品        2.查看购物车        3.我的订单        4.修改密码        5.注销");
		System.out.println("------------------------------------------");
		System.out.print("请选择：");
		int choice = input.nextInt();
		switch(choice){
			case 1:
				buy();
				break;
			case 2:
				showCart();
				break;
			case 3:
				showMyOrder();
				break;
			case 4:
				modifyPwd();
				break;
			case 5:
				new Start().showSystemMenu(); // 返回系统主菜单
				break;
			default:
				System.out.println("输入无效！");
				break;
		}
		
	}

	/*
	 * 购买商品
	 */
	private void buy() {
		System.out.println("----------------------------------------");
		List<Product> products = productService.findAll();
		System.out.println("编号\t商品\t\t价格");
		System.out.println("----------------------------------------");
		for(int i=0;i<products.size();i++){
			Product p = products.get(i);
			System.out.println(p.getId()+"\t"+p.getName()+"\t\t"+p.getPrice());
		}
		System.out.print("请选择要添加到购物车的商品编号：");
		int id = input.nextInt();
		try {
			cart.add(id);
			System.out.println("添加成功！");
		} catch (ProductNotExistsException e) {
			System.out.println("提示：添加失败，"+e.getMessage());
		}
		showMainMenu(); // 返回主菜单
	}

	// 查看购物车
	private void showCart() {
		System.out.println("----------------------------------------");
		System.out.println("编号\t商品\t单价\t数量\t金额");
		System.out.println("----------------------------------------");
		double totalPrice = 0; // 总价
		for(Item item : cart.getItems()){
			Product p = item.getProduct();
			System.out.println(p.getId()+"\t"+p.getName()+"\t"+p.getPrice()+"\t"+item.getNum()+"\t"+item.getPrice());
			totalPrice += item.getPrice();
		}
		System.out.println("----------------------------------------");
		System.out.println("共："+cart.getItems().size()+"件，总计："+totalPrice);
		System.out.println("----------------------------------------");
		System.out.print("请选择（1.删除商品  2.修改商品数量  3.清空购物车  4.结算  5.返回用户主菜单）：");
		int choice = input.nextInt();
		switch(choice){
			case 1:
				removeById();
				break;
			case 2:
				modifyNum();
				break;
			case 3:
				clear();
				break;
			case 4:
				settleAccounts();
				break;
			case 5:
				showMainMenu();
				break;
			default:
				System.out.println("输入无效！");
				break;
		}
		showCart(); // 显示购物车
	}

	// 删除商品
	private void removeById() {
		System.out.println("----------------------------------------");
		System.out.print("请输入要删除的商品编号：");
		cart.removeById(input.nextInt());
		System.out.println("删除成功！");
	}

	// 修改数量
	private void modifyNum() {
		System.out.println("----------------------------------------");
		System.out.print("请输入要修改的商品编号：");
		int id = input.nextInt();
		System.out.print("请输入商品数量：");
		int num = input.nextInt();
		cart.modifyNum(id, num);
		System.out.println("修改成功！");
	}

	// 清空购物车
	private void clear() {
		cart.clear();
		System.out.println("购物车已清空！");
	}

	// 结算
	private void settleAccounts() {
		System.out.println("----------------------------------------");
		System.out.println("订单信息如下：");
		System.out.println("用户："+user.getUsername()+"，电话："+user.getPhone()+"，地址："+user.getAddress());
		System.out.println("编号\t商品\t单价\t数量\t金额");
		System.out.println("----------------------------------------");
		double totalPrice = 0; // 总价
		for(Item item : cart.getItems()){
			Product p = item.getProduct();
			System.out.println(p.getId()+"\t"+p.getName()+"\t"+p.getPrice()+"\t"+item.getNum()+"\t"+item.getPrice());
			totalPrice += item.getPrice();
		}
		System.out.println("----------------------------------------");
		System.out.println("共："+cart.getItems().size()+"件，总计："+totalPrice);
		System.out.println("----------------------------------------");
		System.out.print("确认要提交订单吗？（1.确认 2.返回）：");
		int choice = input.nextInt();
		switch(choice){
			case 1:
				submitOrder();
				break;
			case 2:
				showCart();
				break;
		}
			
	}

	// 提交订单
	private void submitOrder() {
		Order order = new Order();
		order.setUser(user);
		order.setNo(StringUtil.generateNo());
		double totalPrice = 0;
		for(Item item : cart.getItems()){
			totalPrice += item.getPrice();
		}
		order.setPrice(totalPrice);
		order.setCreateDate(new Date());
		order.setItems(new HashSet<Item>(cart.getItems()));
		
		orderService.add(order);
		System.out.println("生成订单成功，订单号："+order.getNo());
		cart.clear(); // 清空购物车
		showMainMenu();
	}

	/*
	 * 我的订单
	 */
	private void showMyOrder() {
		// TODO Auto-generated method stub
		
	}

	/*
	 * 修改密码
	 */
	private void modifyPwd() {
		// TODO Auto-generated method stub
		
	}
	
}
